package com.example.service.impl.ums;

import com.example.common.DozerUtils;
import com.example.common.UUIDUtils;
import com.example.mapper.ums.UmsAdminMapper;
import com.example.mapper.ums.UmsAdminRoleMapper;
import com.example.mapper.ums.UmsRoleMapper;
import com.example.model.ums.UmsAdmin;
import com.example.model.ums.UmsAdminRole;
import com.example.model.ums.UmsRole;
import com.example.model.vo.UmsAdminVo;
import com.example.service.ums.UmsAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 后台登录用户Service
 */
@Service
@Transactional
public class UmsAdminServiceImpl implements UmsAdminService {

    @Autowired
    private UmsAdminMapper mapper;

    @Autowired
    private BCryptPasswordEncoder cryptPasswordEncoder;

    @Autowired
    private UmsAdminRoleMapper umsAdminRoleMapper;

    @Autowired
    private UmsRoleMapper umsRoleMapper;

    @Override
    public PageInfo<UmsAdmin> findUmsAdminsByParam(UmsAdminVo umsAdmin) {
        PageHelper.startPage(umsAdmin.getPage(),umsAdmin.getRows());//分页起始码以及每页页数
        UmsAdmin conversion = DozerUtils.conversion(umsAdmin, UmsAdmin.class);
        List<UmsAdmin> umsAdminsByParam = mapper.findUmsAdminsByParam(conversion);
        PageInfo pageInfo=new PageInfo(umsAdminsByParam);
        return pageInfo;
    }

    @Override
    public UmsAdmin findUmsAdminByParam(UmsAdmin umsAdmin)  {
        UmsAdmin umsAdminByParam = mapper.findUmsAdminByParam(umsAdmin);
        return umsAdminByParam;
    }

    @Override
    public void insert(UmsAdmin umsAdmin) throws Exception {
        umsAdmin.setId(UUIDUtils.getId());
        UmsAdmin umsAdminParam = new UmsAdmin();
        umsAdminParam.setUsername(umsAdmin.getUsername());
        UmsAdmin umsAdminByParam = mapper.findUmsAdminByParam(umsAdminParam);
        if(null == umsAdminByParam){
            umsAdmin.setCreateTime(new Date());
            umsAdmin.setPassword(cryptPasswordEncoder.encode(umsAdmin.getPassword()));
            mapper.insert(umsAdmin);
            //如果是管理员将用户添加管理员权限
            if(umsAdmin.getAdminiStrators() == 2){
                //查询管理员角色
                UmsRole umsRole = new UmsRole();
                umsRole.setRoleCode("superAdministrator");
                UmsRole umsRoleByParam = umsRoleMapper.findUmsRoleByParam(umsRole);
                if(null == umsRoleByParam){
                    return ;
                }
                //为用户分配管理员角色
                UmsAdminRole umsAdminRole = new UmsAdminRole();
                umsAdminRole.setId(UUIDUtils.getId());
                umsAdminRole.setUmsAdminId(umsAdmin.getId());
                umsAdminRole.setRoleId(umsRoleByParam.getId());
                umsAdminRoleMapper.insert(umsAdminRole);
            }
        }else{
            throw new Exception("用户名重复");
        }
    }

    @Override
    public void update(UmsAdmin umsAdmin) throws Exception {
        UmsAdmin umsAdminParam = new UmsAdmin();
        umsAdminParam.setUsername(umsAdmin.getUsername());
        UmsAdmin umsAdminByParam = mapper.findUmsAdminByParam(umsAdminParam);
        if(null == umsAdminByParam){
            umsAdmin.setUpdateTime(new Date());
            mapper.update(umsAdmin);
        }else{
            throw new Exception("用户名重复");
        }

    }

    @Override
    public int deleteById(String id) {
        int deleteById = mapper.deleteById(id);
        if(deleteById == 1){
            UmsAdminRole umsAdminRole = new UmsAdminRole();
            umsAdminRole.setUmsAdminId(id);
            UmsAdminRole umsAdminRoleByParam = umsAdminRoleMapper.findUmsAdminRoleByParam(umsAdminRole);
            if(null != umsAdminRoleByParam){
                umsAdminRoleMapper.deleteByUmsAdminId(umsAdminRole);
            }
        }
        return deleteById;
    }
}
